//-----------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation.  All rights reserved.
//-----------------------------------------------------------------------------

namespace Microsoft.ServiceModel.Web
{
    using System.Threading;
    using System.ServiceModel.Diagnostics;
    using System.Diagnostics;
    using System;
    using System.Collections.ObjectModel;
    using System.ServiceModel.Description;
    using System.ServiceModel.Dispatcher;
    using System.ServiceModel.Channels;
    using System.ServiceModel.Web;
    using System.Xml;
    using System.Text;
    using System.Web;
    using System.Collections.Generic;
    using System.ServiceModel;

    public class WebServiceHost2 : WebServiceHost
    {
        Collection<RequestInterceptor> interceptors = new Collection<RequestInterceptor>();

        public WebServiceHost2(object singletonInstance, params Uri[] baseAddresses)
            : base(singletonInstance, baseAddresses)
        {
        }

        public WebServiceHost2(Type serviceType, params Uri[] baseAddresses)
            : base(serviceType, baseAddresses)
        {
        }

        public Collection<RequestInterceptor> Interceptors { get { return this.interceptors; } }

        protected override void OnOpening()
        {
            base.OnOpening();
            foreach (var ep in this.Description.Endpoints)
            {
                CustomBinding binding = new CustomBinding(ep.Binding);
                if (this.Interceptors.Count > 0)
                {
                    binding.Elements.Insert(0, new RequestInterceptorBindingElement(this.Interceptors));
                }
                ep.Binding =  binding;
            }
        }
    }
}
